package com.itheima.studentsystem;

import java.util.ArrayList;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {

        ArrayList<User> list = new ArrayList<>();
        User u = new User("heima001","123","123","123");
        list.add(u);
        //先验证用户名的格式是否满足要求，在验证用户名是否唯一
        Scanner sc = new Scanner(System.in);
        //判断用户名是否满足要求
        loop:
        while (true) {
            System.out.println("请输入用户名");
            String username = sc.next();
            //判断用户名的长度是否满足要求
            if (!(username.length() >= 3 && username.length() <= 15)) {
                System.out.println("请输入的用户名不满足要求，请重新输入用户名");
                continue;
            }
            //判断用户名是否满足字母加数字的组合
            for (int i = 0; i < username.length(); i++) {
                char c = username.charAt(i);
                if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'))) {
                    System.out.println("请输入的用户名不满足要求，请重新输入用户名");
                    continue loop;
                }
            }
            //判断用户名是否是纯数字
            int count = 0;
            for (int i = 0; i < username.length(); i++) {
                char c = username.charAt(i);
                if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
                    count++;
                    break;
                }
            }

            if (count <= 0) {
                System.out.println("用户名格式不满足要求，请重新输入用户名");
                continue;
            }
            //判断用户名是否唯一
            boolean flag = contains(list,username);
            if (flag){
                System.out.println("用户名已存在，请重新输入用户名");
            }else {
                break ;
            }
        }
    }
    private static boolean contains(ArrayList<User> list, String username) {
        for (int i = 0; i < list.size(); i++) {
            User u = list.get(i);
            if (u.getUsername().equals(username)){
                return true;
            }
        }
        return false;
    }
}
